

const yz = require('../comm/yz');
var moment = require("moment");
var math = require("mathjs");
var clType = require("../dict/clothesSizeType")

exports.fmtCustomerOrder = function (obj) {

    obj = yz.IsNullThen(obj, {});


    obj.name = yz.IsNullThen(obj.name, "");
    obj.customerId = yz.IsNullThen(obj.customerId, "");
    obj.customerName = yz.IsNullThen(obj.customerName, "");
    obj.classId = yz.IsNullThen(obj.classId, "");
    obj.fileList = yz.IsNullThen(obj.fileList, []);
    obj.flow = yz.IsNullThen(obj.flow, {});
    obj.count = yz.IsNullThen(obj.count, {});
    obj.count.reAmount = yz.IsNullThen(obj.count.reAmount, 0);
    obj.count.amount = yz.IsNullThen(obj.count.amount, 0);
    obj.proof = this.fmtData(obj.proof);
    obj.proof.bulkNum = yz.IsNullThen(obj.proof.bulkNum, 0)   //预估大货数量
    obj.bulk = this.fmtData(obj.bulk);
    obj.flow = yz.IsNullThen(obj.flow, {})
    obj.flow.proof = this.fmtFlowData(obj.flow.proof)
    obj.flow.bulk = this.fmtFlowData(obj.flow.bulk)
    obj.openProof = yz.IsNullThen(obj.openProof, false);
    obj.openBulk = yz.IsNullThen(obj.openBulk, false);
    obj.salesMembers = yz.IsNullThen(obj.salesMembers, []);
    obj.statusId = yz.IsNullThen(obj.statusId, 0);
    obj.unitId = yz.IsNullThen(obj.unitId, "");

    return obj



};
exports.technologyLv = function () {
    // body...


    return [
        {
            id: "10",
            title: "S",
            name: "高端定制",
            memo: ""
        }, {
            id: "20",
            title: "A",
            name: "普通精品",
            memo: ""
        }, {
            id: "30",
            title: "B",
            name: "品牌半精品",
            memo: ""
        }, {
            id: "40",
            title: "C",
            name: "电商/市场半精品",
            memo: ""
        }, {
            id: "50",
            title: "D",
            name: "电商/市场普通",
            memo: ""
        },
        {
            id: "60",
            title: "D",
            name: "市场货",
            memo: ""
        },

    ]
};

exports.getTechnologyLvName = function (id) {

    // body...
    var technologyName = ""
    this.technologyLv().forEach(function (technology) {
        if (technology.id == id) {
            technologyName = technology.name
        }
    }, this);
    return technologyName;
};


exports.fmtPrint = function (obj) {
    // body...
    if (!obj) {
        obj = {};
    }

    obj.print = yz.IsNullThen(obj.print, {});
    obj.print.gongyidan = yz.IsNullThen(obj.print.gongyidan, {});
    obj.print.gongyidan.memoArray = yz.IsNullThen(obj.print.gongyidan.memoArray, []);
    for (var index = 0; index < 12; index++) {

        if (!obj.print.gongyidan.memoArray[index]) {
            obj.print.gongyidan.memoArray[index] = {
                key: "",
                value: ""
            }

        }

    }
    obj.print.gongyidan.mfl = yz.IsNullThen(obj.print.gongyidan.mfl, {});   //工艺单中的面辅料
    obj.print.gongyidan.mfl.Prompt = yz.IsNullThen(obj.print.gongyidan.mfl.Prompt, "");   //面辅料特别提示
    obj.print.gongyidan.mfl.treatmentMode = yz.IsNullThen(obj.print.gongyidan.mfl.treatmentMode, {});    //工艺单中的面辅料处理方式
    obj.print.gongyidan.mfl.treatmentMode.isYuSuo = yz.IsNullThen(obj.print.gongyidan.mfl.treatmentMode.isYuSuo, false);    //
    obj.print.gongyidan.mfl.treatmentMode.isMlXiShui = yz.IsNullThen(obj.print.gongyidan.mfl.treatmentMode.isMlXiShui, false);    //
    obj.print.gongyidan.mfl.treatmentMode.isCyXiShui = yz.IsNullThen(obj.print.gongyidan.mfl.treatmentMode.isCyXiShui, false);    //
    

    //报价单逻辑
    obj.print.baojiadan = yz.IsNullThen(obj.print.baojiadan, {});
    debugger
    obj.print.baojiadan.memoArray = yz.IsNullThen(obj.print.baojiadan.memoArray, []);
    for (var index = 0; index < 12; index++) {

        if (!obj.print.baojiadan.memoArray[index]) {
            obj.print.baojiadan.memoArray[index] = {
                key: "",
                value: ""
            }

        }

    }


    return obj

};

exports.fmtFileList = function (fileList) {

    // body...

    if (!yz.fileList.isArray(fileList)) {
        fileList = [];
        return fileList
    }

    fileList.forEach(function (file) {
        file.isImg = yz.isImg(file.url);
        file.ext = yz.fileExt(file.url);
    }, this);
};

exports.fmtFlowData = function (dataObj) {
    // body...

    dataObj = yz.IsNullThen(dataObj, {});


    dataObj.nodeList = yz.IsNullThen(dataObj.nodeList, []);

    if (dataObj.nodeList.length == 0) {
        dataObj.nodeList = [{
            id: "root",
            name: "起始节点",
            taskClassId: "root"
        }]

    }

    return dataObj

};


exports.fmtData = function (dataObj) {
    // body...


    dataObj = clType.formatData(dataObj);
    return dataObj

};

exports.countItemData = function (item) {
    // body...


    var r = {
        num: 0
    };


    if (item) {
        if (yz.isArray(item.data)) {

            item.data.forEach(function (colorItem) {


                if (yz.isArray(colorItem.sizeArray)) {

                    colorItem.sizeArray.forEach(function (sizeItem) {
                        r.num = r.num + parseInt(sizeItem.num)

                    }, this);
                }

            }, this);

        }
    }
    else {
        return r
    }


    return r

};

exports.fmtDetalType = function (customerOrder) {
    // body...
    var customerOrderDetail = []
    if (customerOrder.openBulk) {
        customerOrderDetail = customerOrderDetail.concat(fmtDetalTypeData("bulk", customerOrder))
    }

    if (customerOrder.openProof) {
        customerOrderDetail = customerOrderDetail.concat(fmtDetalTypeData("proof", customerOrder))
    }

    return customerOrderDetail

};

var fmtDetalTypeData = function (flowClassId, customerOrder) {
    // body...
    var customerOrderDetail = [];
    var openStr = "open" + yz.firstUpperCase(flowClassId, customerOrder)
    if (customerOrder[openStr]) {

        if (yz.isArray(customerOrder[flowClassId].t900)) {


            customerOrder[flowClassId].t900.forEach(function (item) {

                item.flowClassId = flowClassId
                customerOrderDetail.push(item);

            }, this);
        }

    }

    return customerOrderDetail
};


exports.getModelNo = function (customerOrder) {
    // body...

    var re = {
        proof: "-",
        bulk: "-"
    }
    try {


        re.proof = customerOrder["proof"].modelNo

    } catch (e) {
        return ""
    }

    try {
        re.bulk = customerOrder["bulk"].modelNo

    } catch (e) {

    }

    if (re.proof == "" || !re.proof) {
        re.proof = "-"
    }
    if (re.bulk == "" || !re.bulk) {
        re.bulk = "-"
    }

    return re

};


exports.getSalesMembers = function (salesMembers) {
    // body...
    if (!salesMembers) {
        return ""
    }
    var w = [];
    salesMembers.forEach(function (element) {
        w.push(element.name)

    }, this);
    return w.join()
};


exports.statusArray = function () {
    // body...
    return [,
        {
            id: 0,
            name: "新创建"
        },
        {
            id: 100,
            name: "未收款"
        },
        {
            id: 200,
            name: "部分已收"
        }, {
            id: 300,
            name: "已结款"
        }

    ]


};



exports.getStatusName = function (statusId) {
    // body...
    var statusName = ""
    this.statusArray().forEach(function (status) {
        if (status.id == statusId) {

            statusName = status.name
        }
    }, this);

    return statusName

};








